﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerUnitsDao : IUnitsDao
    {
        public List<Unit> GetUnits()
        {
            string procedures = "Get_All_Units";

            return Db.ReadList(procedures, true, Make, null);
        }

        public Unit GetUnit(string unitId)
        {
            string procedures = "Get_Unit_ById";

            object[] parms = { "@UnitID", unitId };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_Unit";
            object[] parms = { "@WhereClause", null };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        public List<Unit> GetUnitsByPage(int currentPage)
        {
            string procedures = "Get_Paged_Units";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Unit> GetUnitsByIsActive(bool isActive)
        {
            string procedures = "Get_All_Units_ByIsActive";

            object[] parms = { "@IsActive", isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public void InsertUnit(Unit unit)
        {
            string sql = "Insert_Unit";

            unit.UnitID = Db.Insert(sql, true, Take(unit));
        }

        public void UpdateUnit(Unit unit)
        {
            string sql = "Update_Unit";

            Db.Update(sql, true, Take(unit));
        }

        public void DeleteUnit(Unit unit)
        {
            string sql = "Delete_Unit";

            Db.Update(sql, true, Take(unit.UnitID));
        }

        private object[] Take(string unitId)
        {
            return new object[]  
            {
                "@UnitID", unitId,
            };
        }

        private object[] Take(Unit unit)
        {
            return new object[]  
            {
                "@UnitID", unit.UnitID,
                "@UnitCode", unit.UnitCode,
                "@UnitName", unit.UnitName,
                "@RestaurantID", unit.RestaurantID,
                "@Description", unit.Description,
                "@IsActive", unit.IsActive
            };
        }

        private static Func<IDataReader, Unit> Make = reader =>
            new Unit
            {
                UnitID = reader["UnitID"].AsString(),
                UnitCode = reader["UnitCode"].AsString(),
                UnitName = reader["UnitName"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
